package com.java110.api.listener.ownerRepair;

import com.alibaba.fastjson.JSONObject;
import com.java110.api.listener.AbstractServiceApiListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.dto.RoomDto;
import com.java110.dto.UnitDto;
import com.java110.dto.community.CommunityDto;
import com.java110.dto.fee.FeeAttrDto;
import com.java110.dto.fee.FeeDto;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairUserDto;
import com.java110.dto.unit.FloorAndUnitDto;
import com.java110.intf.common.IFileRelInnerServiceSMO;
import com.java110.intf.community.*;
import com.java110.intf.fee.IFeeInnerServiceSMO;
import com.java110.utils.constant.ServiceCodeOwnerRepairConstant;
import com.java110.utils.constant.StatusConstant;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import com.java110.vo.api.junkRequirement.PhotoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;


/**
 * 查询小区侦听类
 */
@Java110Listener("listOwnerRepairsListener")
public class ListOwnerRepairsListener extends AbstractServiceApiListener {

    @Autowired
    private IRepairInnerServiceSMO repairInnerServiceSMOImpl;


    @Autowired
    private IRepairUserInnerServiceSMO repairUserInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IFeeInnerServiceSMO feeInnerServiceSMOImpl;

    @Autowired
    private ICommunityInnerServiceSMO communityInnerServiceSMOImpl;

    @Autowired
    private IUnitInnerServiceSMO unitInnerServiceSMOImpl;

    @Autowired
    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;


    public IFeeInnerServiceSMO getFeeInnerServiceSMOImpl() {
        return feeInnerServiceSMOImpl;
    }

    public void setFeeInnerServiceSMOImpl(IFeeInnerServiceSMO feeInnerServiceSMOImpl) {
        this.feeInnerServiceSMOImpl = feeInnerServiceSMOImpl;
    }

    @Override
    public String getServiceCode() {
        return ServiceCodeOwnerRepairConstant.LIST_OWNERREPAIRS;
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.GET;
    }


    @Override
    public int getOrder() {
        return DEFAULT_ORDER;
    }

    String  fileBaseUrl = "/callComponent/download/getFile/fileByObjId";
    public IRepairInnerServiceSMO getRepairInnerServiceSMOImpl() {
        return repairInnerServiceSMOImpl;
    }

    public void setRepairInnerServiceSMOImpl(IRepairInnerServiceSMO repairInnerServiceSMOImpl) {
        this.repairInnerServiceSMOImpl = repairInnerServiceSMOImpl;
    }

    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {
        super.validatePageInfo(reqJson);
    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {

        RepairDto ownerRepairDto = BeanConvertUtil.covertBean(reqJson, RepairDto.class);

        if (!StringUtil.isEmpty(ownerRepairDto.getRoomId()) && ownerRepairDto.getRoomId().contains(",")) {
            String[] roomIds = ownerRepairDto.getRoomId().split(",");
            ownerRepairDto.setRoomIds(roomIds);
            ownerRepairDto.setRoomId("");
        }

        int count = repairInnerServiceSMOImpl.queryRepairsCount(ownerRepairDto);


        List<RepairDto> ownerRepairs = null;
        if (count > 0) {
            ownerRepairs = repairInnerServiceSMOImpl.queryRepairs(ownerRepairDto);

            refreshRepair(ownerRepairs);
        } else {
            ownerRepairs = new ArrayList<>();
        }

        ResponseEntity<String> responseEntity = ResultVo.createResponseEntity((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, ownerRepairs);


        context.setResponseEntity(responseEntity);

    }

    private void refreshRepair(List<RepairDto> ownerRepairs) {

        List<String> repairIds = new ArrayList<>();
        for (RepairDto apiOwnerRepairDataVo : ownerRepairs) {
            repairIds.add(apiOwnerRepairDataVo.getRepairId());

            String communityId = apiOwnerRepairDataVo.getCommunityId();
            //刷入完整地址信息 //001 小区 002 楼栋 003 单元 004 房屋
            if(RepairDto.OBJ_TYPE_COMMUNITY.equals(apiOwnerRepairDataVo.getRepairObjType())){//001 小区 002 楼栋 003 单元 004 房屋
                //001 小区
                apiOwnerRepairDataVo.setAddress(apiOwnerRepairDataVo.getRepairObjName());
            }else if(RepairDto.OBJ_TYPE_FLOOR.equals(apiOwnerRepairDataVo.getRepairObjType())){
                //002 楼栋
                String communityName = getComunityNameById(communityId);
                String buildingName = apiOwnerRepairDataVo.getRepairObjName()+"栋";
                apiOwnerRepairDataVo.setAddress(communityName+buildingName);
            }else if(RepairDto.OBJ_TYPE_UNIT.equals(apiOwnerRepairDataVo.getRepairObjType())){
                //003 单元
                String communityName = getComunityNameById(communityId);
                String floorAndUnitName = getFloorAndUnitNameByUnitId(apiOwnerRepairDataVo);
                apiOwnerRepairDataVo.setAddress(communityName+floorAndUnitName);
            }else{
                //004 房屋
                String communityName = getComunityNameById(communityId);
                String floorAndUnitAndRoomName = getFloorAndUnitAndRoomNameByRoomId(apiOwnerRepairDataVo);
                apiOwnerRepairDataVo.setAddress(communityName+floorAndUnitAndRoomName);
            }

            //刷入订单金额
            if(RepairDto.STATE_PAY.equals(apiOwnerRepairDataVo.getState())){
                FeeAttrDto feeAttrDto = new FeeAttrDto();
                feeAttrDto.setCommunityId(apiOwnerRepairDataVo.getCommunityId());
                feeAttrDto.setSpecCd(FeeAttrDto.SPEC_CD_REPAIR);
                feeAttrDto.setValue(apiOwnerRepairDataVo.getRepairId());
                feeAttrDto.setPage(0);
                feeAttrDto.setRow(1);
                List<FeeDto> feeDtos = feeInnerServiceSMOImpl.queryFeeByAttr(feeAttrDto);
                if(feeDtos.size()>0){
                    apiOwnerRepairDataVo.setOweFee(feeDtos.get(0).getAmount());
                }
            }
        }

        if (repairIds.size() < 1) {
            return;
        }
        RepairUserDto repairUserDto = new RepairUserDto();
        repairUserDto.setRepairIds(repairIds.toArray(new String[repairIds.size()]));
        repairUserDto.setRepairEvent(RepairUserDto.REPAIR_EVENT_AUDIT_USER);
        List<RepairUserDto> repairUserDtos = repairUserInnerServiceSMOImpl.queryRepairUsers(repairUserDto);

        for (RepairUserDto tmpRepairUserDto : repairUserDtos) {
            for (RepairDto apiOwnerRepairDataVo : ownerRepairs) {
                if (tmpRepairUserDto.getRepairId().equals(apiOwnerRepairDataVo.getRepairId())) {
                    apiOwnerRepairDataVo.setStaffId(tmpRepairUserDto.getStaffId());
                    //刷入员工照片
                    String photoUrl = fileBaseUrl + "?objId="+
                            tmpRepairUserDto.getStaffId() +
                            "&communityId="+tmpRepairUserDto.getCommunityId() +
                            "&fileTypeCd=12000";

                    apiOwnerRepairDataVo.setStaffPhoto(photoUrl);
                    //刷入员工电话
                    apiOwnerRepairDataVo.setStaffPhone(tmpRepairUserDto.getStaffPhone());
                }
            }
        }

        //刷入图片信息
        List<PhotoVo> photoVos = null;
        PhotoVo photoVo = null;
        for (RepairDto repairDto : ownerRepairs) {
            FileRelDto fileRelDto = new FileRelDto();
            fileRelDto.setObjId(repairDto.getRepairId());
            List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
            photoVos = new ArrayList<>();
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                photoVo = new PhotoVo();
                photoVo.setUrl("/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=" + repairDto.getCommunityId());
                photoVos.add(photoVo);
            }

            repairDto.setPhotos(photoVos);
        }

    }

    private String getFloorAndUnitAndRoomNameByRoomId(RepairDto apiOwnerRepairDataVo) {
        String roomId = apiOwnerRepairDataVo.getRepairObjId();
        RoomDto roomDto = new RoomDto();
        roomDto.setRoomId(roomId);
        List<RoomDto> roomInfos = roomInnerServiceSMOImpl.getRoomInfo(roomDto);
        String floorAndUnitAndRoomName = "";
        if(roomInfos.size()>0){
            floorAndUnitAndRoomName = roomInfos.get(0).getFloorNum()+"栋"+roomInfos.get(0).getUnitNum()+"单元"+roomInfos.get(0).getRoomNum();
        }
        return floorAndUnitAndRoomName;
    }

    private String getFloorAndUnitNameByUnitId(RepairDto apiOwnerRepairDataVo) {
        String unitId = apiOwnerRepairDataVo.getRepairObjId();
        FloorAndUnitDto floorAndUnitDto = new FloorAndUnitDto();
        floorAndUnitDto.setUnitId(unitId);
        List<FloorAndUnitDto> floorAndUnitInfos = unitInnerServiceSMOImpl.getFloorAndUnitInfo(floorAndUnitDto);
        String floorAndUnitName = "";
        if(floorAndUnitInfos.size()>0){
            floorAndUnitName = floorAndUnitInfos.get(0).getFloorNum()+"号楼"+floorAndUnitInfos.get(0).getUnitNum()+"单元";
        }
        return floorAndUnitName;
    }

    private String getComunityNameById(String communityId) {
        CommunityDto communityDto = new CommunityDto();
        communityDto.setCommunityId(communityId);
        communityDto.setStatusCd(StatusConstant.STATUS_CD_VALID);
        List<CommunityDto> communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
        String communityName = "";
        if(communityDtos.size()>0){
            communityName = communityDtos.get(0).getName();
        }
        return communityName;
    }


}
